package com.hnkywl.sts.service.organ;

import com.hnkywl.sts.dao.organ.YsgjDao;
import com.hnkywl.sts.entity.organ.Qyjg;
import com.hnkywl.sts.entity.organ.Wls;
import com.hnkywl.sts.entity.organ.Ysgj;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-29
 * Time: 下午3:32
 * To change this template use File | Settings | File Templates.
 */
@Service
public class YsgjService {
    private YsgjDao ysgjDao;
    @Transactional
    public String findXm(Long wlsId,int pageNo, int pageSize){
        Page<Ysgj> YsgjPage = this.ysgjDao.findYsgjList(wlsId,pageNo, pageSize);
        List<Ysgj> YsgjList = YsgjPage.getResult() == null?new ArrayList<Ysgj>():YsgjPage.getResult();
        String YsgjJson = BaseUtils.fetchCollectionToJson(YsgjList, "id","ysgjlx.id","ysgjlx.z","ysgjlx.bz","ysgjsbm","gps","xm","sjhm","wls.id","wls.qc");
        return "[" + YsgjJson + "]";
    }
    @Transactional
    public void saveYsgj(Ysgj ysgj) throws RuntimeException {
        if (this.ysgjDao.checkQc(ysgj.getId(),ysgj.getYsgjsbm())) {
            throw new RuntimeException("运输工具已存在！");
        }
        ysgj.setDateCreated(new Date());
        ysgj.setLastUpdated(ysgj.getDateCreated());
        this.ysgjDao.save(ysgj);
    }

    @Transactional
    public void deleteYsgj(Long  id) {
        Ysgj ysgj = ysgjDao.get(id);
        ysgj.setLastUpdated(new Date());
        ysgj.setDeleted(1);
        this.ysgjDao.save(ysgj);
    }
    //========================get and set ==============================
    @Autowired
    public void setYsgjDao(YsgjDao ysgjDao) {
        this.ysgjDao = ysgjDao;
    }
}
